package com.luoy.hadoop;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.IOException;
import java.net.URI;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName com.sugon.HdfsGetLatest
 * @Description TODO
 * @Author wyc
 * @Date 2022/7/11 11:33
 * @Version 1.0
 **/
public class HdfsGetLatest {
    private static FileSystem fs;

    private static Map<String,Long> fileMappings = new HashMap<>();
    public static void init(){
        URI uri = URI.create("hdfs://172.22.5.12");
        //conf配置对象
        Configuration conf = new Configuration();
//        conf.set("dfs.replication","6");
        //操作的用户(用那个用户操作HDFS)
        String user = "hadoop";
        //获取hdfs的客服端连接对象（文件系统对象）
        try {
            try {
                fs = FileSystem.get(uri ,conf,user);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void ListDir(String path) throws IOException {
//        System.out.println("listdir:"+path);
        FileStatus[] listStatus = fs.listStatus(new Path(path));

        for (FileStatus status: listStatus) {
            if(status.isDirectory()){
                Long timestamp = status.getModificationTime();
//                    System.out.println("文件修改时间:" + timestamp);
                    String tmpStr = new SimpleDateFormat("yyyyMMddHHmmss").format(timestamp);

                    String dbName = path.split("/")[3];
                    String tableName = status.getPath().getName();
//                    System.out.println(String.format("drop table %s.%s", dbName,tableName));

//                    System.out.println(path + "/" +status.getPath().getName() +" "+tmpStr);

                ListDir(path+"/"+status.getPath().getName());
//                System.out.println("DIR:\t"+path + "/" +status.getPath().getName());
            }
            else {
//                System.out.println("FILE:\t"+status.getPath().getName());
//                System.out.println("文件修改时间:"+status.getModificationTime());
                Long timestamp = status.getModificationTime();
//                    System.out.println("文件修改时间:" + timestamp);
                String filename = status.getPath().getName();
                if(filename.contains("_metadata")) {
                    System.out.println(path + "/" + status.getPath().getName()+" "+timestamp.toString());
                }
            }
        }
    }

    public static void main(String[] args) throws IOException {
        init();
        ListDir("/CheckPoint/274/401/cdc_19");

    }
}
